package com.youmingxing.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.Const;

import org.json.JSONObject;

/**
 * JHS
 */

public class ShareDialog extends BaseActivity implements View.OnClickListener {

    private TextView cancleBtn;
    private LinearLayout qqShare;
    private LinearLayout wechatShare;
    private LinearLayout wechatFriendShare;
    private LinearLayout microblogShare;
    UMSocialService mController;
    int entrance;//来源页面
    /*
     entrance为1时，传用户ID；
     entrance为2时，传 海选ID；
     entrance为3时，传 发布的动态ID
 */
    int id;//用户id,海选id,或作品id
    int type;//qq,weixin,weibo....
    final int TYPE_QQ = 3;
    final int TYPE_WEIXIN = 1;
    final int TYPE_CIRCLE = 2;
    final int TYPE_WEIBO = 4;
    String timestamp;//服务器返回的时间戳,分享成功后发回去



    public void onStart() {
        super.onStart();
        final DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        final WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = dm.widthPixels;
        layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.dialog_four_items);
        layoutParams.gravity = Gravity.BOTTOM;

        getWindow().setAttributes(layoutParams);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share);
        id = getIntent().getExtras().getInt("id");
        entrance = getIntent().getExtras().getInt("entrance");
        mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        mController.getConfig().closeToast();
        //设置新浪SSO handler
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        //qq
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, Const.APPID_QQ,
                Const.APP_KEY_QQ);
        qqSsoHandler.addToSocialSDK();
        //wx
        UMWXHandler wxHandler = new UMWXHandler(this, Const.APPID_WEIXIN, Const.SECRET_WEIXIN);
        wxHandler.addToSocialSDK();
        //wx friend circle
        UMWXHandler wxCircleHandler = new UMWXHandler(this, Const.APPID_WEIXIN, Const.SECRET_WEIXIN);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
        Log.LOG = true;
        initViewTest();
    }


    private void initViewTest() {
        qqShare = (LinearLayout) findViewById(R.id.qq_ll);
        wechatShare = (LinearLayout) findViewById(R.id.wechat_ll);
        wechatFriendShare = (LinearLayout) findViewById(R.id.wechat_friend_ll);
        microblogShare = (LinearLayout) findViewById(R.id.microblog_ll);
        cancleBtn = (TextView) findViewById(R.id.cancel_btn);
        cancleBtn.setOnClickListener(this);
        microblogShare.setOnClickListener(this);
        wechatShare.setOnClickListener(this);
        wechatFriendShare.setOnClickListener(this);
        qqShare.setOnClickListener(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qq_ll:
                shareContent(TYPE_QQ, SHARE_MEDIA.QQ);
                break;
            case R.id.wechat_ll:
                shareContent(TYPE_WEIXIN, SHARE_MEDIA.WEIXIN);
                break;
            case R.id.wechat_friend_ll:
                shareContent(TYPE_CIRCLE, SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.microblog_ll:
                shareContent(TYPE_WEIBO, SHARE_MEDIA.SINA);
                break;
            case R.id.cancel_btn:
                finish();
                break;
            default:
                break;
        }
    }

    void shareContent(final int type, final SHARE_MEDIA share_media) {
        this.type = type;
        showDialog("分享中");
        SelfInfoApi.getShareContent(type, id, entrance, new BaseJsonHandler(this) {
            @Override
            public void onSuccessWithData(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                timestamp = data.optString("_r");
                switch (type){
                    case TYPE_QQ:
                        QQShareContent qqShareContent = new QQShareContent();
                        qqShareContent.setShareContent(data.optString("title"));
                        qqShareContent.setShareImage(new UMImage(ShareDialog.this, data.optString
                                ("img")));
                        qqShareContent.setTargetUrl(data.optString("link"));
                        mController.setShareMedia(qqShareContent);
                        mController.postShare(ShareDialog.this, share_media, mShareListener);
                        break;
                    case TYPE_WEIXIN:
                        WeiXinShareContent weiXinShareContent = new WeiXinShareContent();
                        weiXinShareContent.setShareContent(data.optString("title"));
                        weiXinShareContent.setShareImage(new UMImage(ShareDialog.this, data.optString
                                ("img")));
                        weiXinShareContent.setTargetUrl(data.optString("link"));
                        mController.setShareMedia(weiXinShareContent);
                        mController.postShare(ShareDialog.this, share_media, mShareListener);
                        break;
                    case TYPE_CIRCLE:
                        CircleShareContent circleShareContent = new CircleShareContent();
                        circleShareContent.setShareContent(data.optString("title"));
                        circleShareContent.setTitle(data.optString("title"));
                        circleShareContent.setShareImage(new UMImage(ShareDialog.this, data.optString
                                ("img")));
                        circleShareContent.setTargetUrl(data.optString("link"));
                        mController.setShareMedia(circleShareContent);
                        mController.postShare(ShareDialog.this, share_media, mShareListener);
                        break;
                    case TYPE_WEIBO:
                        SinaShareContent sinaShareContent = new SinaShareContent();
                        sinaShareContent.setShareContent(data.optString("title")+data.optString
                                ("link"));
                        sinaShareContent.setShareImage(new UMImage(ShareDialog.this, data.optString
                                ("img")));
                        mController.setShareMedia(sinaShareContent);
                        mController.postShare(ShareDialog.this, share_media, mShareListener);
                        break;
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }


    /**
     * 分享监听器
     */
    SocializeListeners.SnsPostListener mShareListener = new SocializeListeners.SnsPostListener() {

        @Override
        public void onStart() {}

        @Override
        public void onComplete(SHARE_MEDIA platform, int stCode,
                               SocializeEntity entity) {
            if (stCode == 200) {
                hideDialog();
                Toast.makeText(ShareDialog.this, "分享成功", Toast.LENGTH_SHORT)
                        .show();
                SelfInfoApi.shareSuccessCallback(type, id, entrance, timestamp, new
                        BaseJsonHandler(ShareDialog.this) {
                            @Override
                            public void onSuccessWithData(JSONObject response) {
                                finish();
                            }
                        });

            } else {
                hideDialog();
            }
        }
    };


}
